import axios, { $post, $get, $download } from "@/utils/axios";
import mixin from "@/mixins/mixin.js";
import _cookie from "@/utils/cookie";
import moment from "moment";
import { saveAs } from "file-saver";
export default (Vue, vm) => {
  Vue.prototype.$mixin = mixin;
  Vue.prototype.$ajax = axios;
  Vue.prototype.$post = $post;
  Vue.prototype.$download = $download;
  Vue.prototype.$get = $get;
  Vue.prototype.COOKIE = _cookie;

  Vue.prototype.fileExport = (res, name = "模板", fileType = ".xlsx") => {
    saveAs(new Blob([res]), `${name}${fileType}`);
  };
  Vue.prototype.$message = params => {
    if (typeof params === "string") {
      let str = params;
      params = {
        message: str
      };
    }
    if (typeof params.duration === "undefined") {
      params.duration = 3000;
    }
    $message(params);
  };

  Vue.prototype.thousands = (num, len) => {
    try {
      if (!num) return "0.00";
      if (len) {
        return `${(num * 1).toFixed(len)}`.replace(
          /\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,
          "$&,"
        );
      } else {
        return `${(num * 1).toFixed(2)}`.replace(
          /\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,
          "$&,"
        );
      }
    } catch (error) {
      return num;
    }
  };

  Vue.prototype.dateYMDHMSFormat = (
    dateStr,
    pattern = "YYYY-MM-DD HH:mm:ss"
  ) => {
    if (!dateStr) return "";
    return moment(dateStr).format(pattern);
  };
  Vue.prototype.translateNumber = num => {
    try {
      if (num) {
        const val = num.replace(/,/g, "");
        return val * 1;
      }
      return "";
    } catch (error) {
      return num;
    }
  };

  //element 时间
  Vue.prototype.pickerOptions = (type = 1) => {
    return {
      disabledDate(time) {
        return {
          1: val => {
            // 只能选择当前及以后的日期
            return val.getTime() < Date.now() - 8.64e7;
          },
          2: val => {
            // 只能选择今天以及今天以前的日期
            return val.getTime() > Date.now() - 8.64e6;
          },
          3: val => {
            // 只能选择今天之后的日期
            return val.getTime() < Date.now();
          },
          4: val => {
            // 只能选择今天之前的日期
            return val.getTime() > Date.now();
          },
          5: val => {
            // 设置选择三个月之前到今天的日期
            let curDate = new Date().getTime();
            let three = 90 * 24 * 3600 * 1000;
            let threeMonths = curDate - three;
            return val.getTime() > Date.now() || val.getTime() < threeMonths;
          }
        }[type * 1](time);
      }
    };
  };
};
